package com.lefeng.cockpit.datax.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.didiglobal.logi.security.common.constant.FieldConstant;
import com.didiglobal.logi.security.dao.impl.BaseDaoImpl;
import com.lefeng.cockpit.common.bean.dto.datasource.DataSourceDTO;
import com.lefeng.cockpit.common.bean.po.datasource.DataSourcePO;
import com.lefeng.cockpit.common.bean.vo.datasource.DataSourceVO;
import com.lefeng.cockpit.datax.dao.DataSourceDao;
import com.lefeng.cockpit.datax.dao.mapper.DataSourceMapper;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author weifw
 */
@Component
public class DataSourceDaoImpl extends BaseDaoImpl<DataSourcePO> implements DataSourceDao {

    private final static String ID = "id";
    private static final String DATASOURCE_NAME = "ds_name";
    private static final String STATUS = "status";
    private static final String DS_TYPE = "ds_type";

    @Resource
    private DataSourceMapper dataSourceMapper;

    @Override
    public int insert(DataSourcePO dataSource) {
        return dataSourceMapper.insert(dataSource);
    }

    @Override
    public IPage<DataSourcePO> selectPage(DataSourceDTO queryDTO) {
        IPage<DataSourcePO> page = new Page<>(queryDTO.getPage(), queryDTO.getSize());
        QueryWrapper<DataSourcePO> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like(queryDTO.getDsName() != null, DATASOURCE_NAME, queryDTO.getDsName())
                .eq(queryDTO.getDsType() != null, DS_TYPE, queryDTO.getDsType())
                .orderByDesc(FieldConstant.CREATE_TIME);

        dataSourceMapper.selectPage(page, queryWrapper);
        page.setTotal(dataSourceMapper.selectCount(queryWrapper));
        return page;
    }

    @Override
    public DataSourcePO getByDataSourceName(String sourceName) {
        QueryWrapper<DataSourcePO> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(sourceName != null, DATASOURCE_NAME, sourceName);
        return dataSourceMapper.selectOne(queryWrapper);
    }

    @Override
    public DataSourcePO getById(Integer id) {
        return dataSourceMapper.selectById(id);
    }

    @Override
    public int deleteById(Integer id) {
        return dataSourceMapper.deleteById(id);
    }

    @Override
    public int updateById(DataSourcePO dataSourcePO) {
        return dataSourceMapper.updateById(dataSourcePO);
    }

    @Override
    public int update(DataSourcePO dataSourcePO) {
        QueryWrapper<DataSourcePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ID, dataSourcePO.getId());
        return dataSourceMapper.update(dataSourcePO, queryWrapper);
    }

    @Override
    public List<String> listDistinctTypes() {
        QueryWrapper<DataSourcePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct " + DS_TYPE);
        List<DataSourcePO> configPOS = dataSourceMapper.selectList(queryWrapper);

        if (!CollectionUtils.isEmpty(configPOS)) {
            return configPOS.stream().map(DataSourcePO::getDsType).collect(Collectors.toList());
        }

        return new ArrayList<>();
    }

    @Override
    public List<DataSourcePO> getAllDataSource(DataSourceDTO dataSourceDTO) {
        LambdaQueryWrapper<DataSourcePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataSourcePO::getDsType, dataSourceDTO.getDsType());
        return dataSourceMapper.selectList(queryWrapper);
    }

}
